package com.zhongfl.crm.service.impl.boss;

import com.zhongfl.crm.enums.boss.SequenceTypeEnum;
import com.zhongfl.crm.service.boss.SequenceAtomService;
import com.zhongfl.crm.service.boss.SequenceService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.LocalDate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.NumberFormat;

/**
 * 序列号生产器
 * 用户系统中编号需要保证唯一，并且呈递增序列的单号
 *
 * @author wangkun23
 * @date 2017/9/7
 */
@Slf4j
@Service
public class SequenceServiceImpl implements SequenceService {

    @Resource
    private SequenceAtomService sequenceAtomService;

    /**
     * 生成订单编号规则，保证不重复
     *
     * @return
     */
    @Override
    public String generateCustomerOrderNo() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("KH");
        stringBuilder.append(LocalDate.now().toString("yyMMdd"));

        NumberFormat formatter = NumberFormat.getNumberInstance();
        formatter.setMinimumIntegerDigits(4);
        formatter.setGroupingUsed(false);
        String nextId = formatter.format(getNextId(SequenceTypeEnum.CUSTOMER_ORDER_NO));
        //这里保证不重复 ，
        stringBuilder.append(nextId);
        stringBuilder.append(RandomStringUtils.randomNumeric(2));
        return stringBuilder.toString();
    }

    /**
     * 生成订单编号规则，保证不重复
     *
     * @return
     */
    @Override
    public String generateDeliverOrderNo() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("JF");
        stringBuilder.append(LocalDate.now().toString("yyMMdd"));

        NumberFormat formatter = NumberFormat.getNumberInstance();
        formatter.setMinimumIntegerDigits(4);
        formatter.setGroupingUsed(false);
        String nextId = formatter.format(getNextId(SequenceTypeEnum.ORDER_NO));
        //这里保证不重复 ，
        stringBuilder.append(nextId);
        stringBuilder.append(RandomStringUtils.randomNumeric(2));
        return stringBuilder.toString();
    }

    /**
     * 生成工单号编号
     */
    @Override
    public String generateWordOrderNo(String code) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("G");
        stringBuilder.append(LocalDate.now().toString("yyMMdd"));
        NumberFormat formatter = NumberFormat.getNumberInstance();
        formatter.setMinimumIntegerDigits(4);
        formatter.setGroupingUsed(false);
        String nextId = formatter.format(getNextId(SequenceTypeEnum.WORK_ORDER_NO));
        //这里保证不重复
        if (!StringUtils.isEmpty(code)) {
            stringBuilder.append(code);
        } else {
            stringBuilder.append("0000");
        }
        stringBuilder.append(nextId);
        return stringBuilder.toString();
    }

    /**
     * 生成任务编号
     */
    @Override
    public String generateTaskNo() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("T");
        stringBuilder.append(LocalDate.now().toString("yyMMdd"));
        NumberFormat formatter = NumberFormat.getNumberInstance();
        formatter.setMinimumIntegerDigits(4);
        formatter.setGroupingUsed(false);
        String nextId = formatter.format(getNextId(SequenceTypeEnum.TASK_NO));
        stringBuilder.append(nextId);
        return stringBuilder.toString();
    }

    @Override
    public String generateTradeNo() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("C");
        stringBuilder.append(LocalDate.now().toString("yyMMdd"));
        NumberFormat formatter = NumberFormat.getNumberInstance();
        formatter.setMinimumIntegerDigits(4);
        formatter.setGroupingUsed(false);
        String nextId = formatter.format(getNextId(SequenceTypeEnum.TRADE_NO));
        stringBuilder.append(nextId);
        return stringBuilder.toString();
    }

    @Override
    public String generateSettleNo() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("S");
        stringBuilder.append(LocalDate.now().toString("yyMMdd"));
        NumberFormat formatter = NumberFormat.getNumberInstance();
        formatter.setMinimumIntegerDigits(4);
        formatter.setGroupingUsed(false);
        String nextId = formatter.format(getNextId(SequenceTypeEnum.TRADE_NO));
        stringBuilder.append(nextId);
        return stringBuilder.toString();
    }

    @Override
    public Integer getNextId(SequenceTypeEnum sequenceTypeEnum) {
        return sequenceAtomService.getNextId(sequenceTypeEnum);
    }


    /**
     * 生成审批编号
     *
     * @return
     */
    @Override
    public String generateApprovalNo() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(LocalDate.now().toString("yyyyMMdd"));
        NumberFormat formatter = NumberFormat.getNumberInstance();
        formatter.setMinimumIntegerDigits(4);
        formatter.setGroupingUsed(false);
        String nextId = formatter.format(getNextId(SequenceTypeEnum.APPROVAL_NO));
        stringBuilder.append(nextId);
        return stringBuilder.toString();
    }
}
